package net.maku.kyy.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.kyy.service.KyyForumReplayService;
import net.maku.kyy.query.KyyForumReplayQuery;
import net.maku.kyy.vo.KyyForumReplayVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 回复表
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/kyy/replay")
@Tag(name="回复表")
@AllArgsConstructor
public class KyyForumReplayController {
    private final KyyForumReplayService kyyForumReplayService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<KyyForumReplayVO>> page(@ParameterObject @Valid KyyForumReplayQuery query){
        PageResult<KyyForumReplayVO> page = kyyForumReplayService.page(query);

        return Result.ok(page);
    }


    /**
     * 查询个人回复
     * @param query
     * @return
     */
    @GetMapping("/about/me")
    public Result<PageResult<KyyForumReplayVO>> replayAboutMe(@ParameterObject @Valid KyyForumReplayQuery query){
        PageResult<KyyForumReplayVO> page = kyyForumReplayService.replayAboutMe(query);
        return Result.ok(page);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<KyyForumReplayVO> get(@PathVariable("id") Long id){
        KyyForumReplayVO data = kyyForumReplayService.get(id);

        return Result.ok(data);
    }

    /**
     * 发表评论
     * @param vo
     * @return
     */
    @PostMapping("/push")
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    public Result<String> push(@RequestBody KyyForumReplayVO vo){
        String result= HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxeb51ae4d4753d3bd&secret=74706deff320b23a81c3b24f7a3f4b57");
        String access_token = JSON.parseObject(result).getString("access_token");
        JSONObject bodyData =new JSONObject();
        bodyData.put("content",vo.getContent());
        bodyData.put("version",2);
        bodyData.put("scene",2);
        bodyData.put("openid",vo.getOpenid());
        String result2= HttpUtil.post("https://api.weixin.qq.com/wxa/msg_sec_check?access_token="+access_token,bodyData.toString());
        String suggest = JSON.parseObject(result2).getJSONObject("result").getString("suggest");
        if("pass".equals(suggest)){
            kyyForumReplayService.save(vo);
            return Result.ok("发布成功");
        }else{
            return Result.ok("禁止发表非法内容，如多次违反将永久封禁");
        }

//        kyyForumReplayService.comment(vo);
//        return Result.ok("发布成功");

    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('kyy:replay')")
    public Result<String> save(@RequestBody KyyForumReplayVO vo){
        kyyForumReplayService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('kyy:replay')")
    public Result<String> update(@RequestBody @Valid KyyForumReplayVO vo){
        kyyForumReplayService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('kyy:replay')")
    public Result<String> delete(@RequestBody List<Long> idList){
        kyyForumReplayService.delete(idList);

        return Result.ok();
    }




    @GetMapping("export")
    @Operation(summary = "导出")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('kyy:replay')")
    public void export() {
        kyyForumReplayService.export();
    }
}